package com.jinzhi.jzweb.controller;


import java.util.*;

import cn.hutool.core.date.DateTime;
import com.jinzhi.jzapi.domain.UserDO;
import com.jinzhi.jzweb.domain.JazSysProjectDO;
import com.jinzhi.jzweb.domain.JazUserAuntDO;
import com.jinzhi.jzweb.service.JazUserAuntService;
import com.jinzhi.sys.dao.UserDao;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jinzhi.common.annotation.Log;
import com.jinzhi.common.base.BaseController;
import com.jinzhi.jzweb.domain.JazSysLabelDO;
import com.jinzhi.jzweb.service.JazSysLabelService;
import com.jinzhi.common.utils.Result;

/**
 * 
 * <pre>
 * 标签管理
 * </pre>
 * <small> 2023-04-18 09:06:15 | Long</small>
 */
@Controller
@RequestMapping("/jzweb/jazSysLabel")
public class JazSysLabelController extends BaseController {
	@Autowired
	private JazSysLabelService jazSysLabelService;

	@Autowired
	private JazUserAuntService jazUserAuntService;
	
	@GetMapping()
	@RequiresPermissions("jzweb:jazSysLabel:jazSysLabel")
	String JazSysLabel(){
	    return "jzweb/jazSysLabel/jazSysLabel";
	}


	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("jzweb:jazSysLabel:jazSysLabel")
	public Result<Page<JazSysLabelDO>> list(JazSysLabelDO jazSysLabelDTO,String current,String size){
		// 查询列表数据
		Page<JazSysLabelDO> page1 = new Page<JazSysLabelDO>(Integer.valueOf(current),Integer.valueOf(size));
		Page<JazSysLabelDO> page = jazSysLabelService.selectPage(page1,
					jazSysLabelService.convertToEntityWrapper().orderBy("id", false));
		List list = new ArrayList();
		for (int i = 0; i < page.getRecords().size(); i++) {
			JazSysLabelDO obj = (JazSysLabelDO) page.getRecords().get(i);
			obj.setCreateTime(DateTime.of(obj.getCreateTime()));
			obj.setNumber(jazUserAuntService.selectCount(new EntityWrapper<JazUserAuntDO>().
					like("label_id", String.valueOf(page.getRecords().get(i).getId()))));
			list.add(obj);
		}
		page.setRecords(list);
        return Result.ok(page);
	}


	@GetMapping("/add")
	@RequiresPermissions("jzweb:jazSysLabel:add")
	String add(){
	    return "jzweb/jazSysLabel/add";
	}
	
	@Log(value = "标签管理-添加标签",sysModule = 9,operationType = 1)
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("jzweb:jazSysLabel:add")
	public Result<String> save( JazSysLabelDO jazSysLabel){
		//设置标签创建人、创建时间
		jazSysLabel.setCreateTime(new DateTime());
		jazSysLabel.setSysUserid(getUserId());
		//默认状态启动
		jazSysLabel.setStatus(jazSysLabel.getStatus());
		jazSysLabelService.insert(jazSysLabel);
        return Result.ok();
	}


	@GetMapping("/edit/{id}")
	@RequiresPermissions("jzweb:jazSysLabel:edit")
	String edit(@PathVariable("id") Long id,Model model){
		JazSysLabelDO jazSysLabel = jazSysLabelService.selectById(id);
		model.addAttribute("jazSysLabel", jazSysLabel);
		return "jzweb/jazSysLabel/edit";
	}
	
	@Log(value = "标签管理-编辑并更新标签数据",sysModule = 9,operationType = 3)
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("jzweb:jazSysLabel:edit")
	public Result<String>  update( JazSysLabelDO jazSysLabel){
		//更新修改时间
		jazSysLabel.setUpdateTime(new DateTime());
		boolean update = jazSysLabelService.updateById(jazSysLabel);
		return update ? Result.ok() : Result.fail();
	}
	
	@Log(value = "标签管理删除标签",sysModule = 9,operationType = 2)
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("jzweb:jazSysLabel:remove")
	public Result<String>  remove( Long id){
		jazSysLabelService.deleteById(id);
        return Result.ok();
	}
	
}
